Creating cool Web pages with Perl by Jerry Muelver

Creating cool Web pages with Perl by Jerry Muelver

Author:Jerry Muelver
Language: eng
Format: epub
Tags: Perl (Computer program language), World Wide Web.
Publisher: IDG Books Worldwide
Published: 1996-06-14T16:00:00+00:00


Creating Cool Web Pages with Perl

The steps for using global variables with subroutines in monum.pl are clear:

1. Assign a value to $monthNum as a test.

2. Assign a value to $myMonth outside of the subroutine.

3. Call up &moNum to start the subroutine code. The subroutine calculates a new value for $monthNum by using the value in $myMonth.

4. Check for the results by printing $monthNum.

Relying on global variables to communicate with a subroutine works and is great for particular applications, but it also produces some side-effects. First, you are restricted to using the exact variable names used within the subroutine. That fact sends you searching through your code to make sure that you have everything exactly right. Second, you are prohibited from using the subroutine's variables anywhere that could accidentally affect the results of subroutine calls from somewhere else. That restriction sends you searching through your code again.

Receiving function values through return

The data hand-off mechanism for subroutines is the reserved word return. The return function takes a value and passes it back as an assignable value to the statement that called the subroutine. The syntax is

return val;

where val is either a variable such as $monthNumor a literal value such as "1' or "All done."

To use return in &moNum, hand over the month number stored in $monthNum, using the line shown in boldface:

sub moNum f

Smonths = "JanFebMarAprMayJunJulAugSepOctNovDec SmonthNum = (index($months. SmyMonth) +3) / 3; return SmonthNum;

Now that &moNum returns a value, you are free to use any variable you want to receive the results of the month-to-number conversion. For example



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.